//获取应用实例
var app = getApp()
//center.js
var currentLocation="";
var verificationCodeInSession="";
var wait = 60;
var flag = 0;
var phone;
var verificationCode;
var areaId="";
var bol;

//获取用户信息
 var getWishUser=function(that){
     wx.request({
       url: app.globalData.path +'/weixin/coffee/xcx/user/getUserInfo',
        data: {
          userId: that.data.userInfo.userId
        },
        method: 'GET',
        success: function (res) {
          console.info(res.data);
          if(res.data.isMember==true && res.data.phone==""){
               that.setData({
                    bangdingHidden:false
               })  
          }
          that.setData({
             wishUser:res.data
          })  
        }
      })
  }
 var checkPhone=function(that){
			var reg=/^[1][34578]\d{9}$/; //验证手机号格式
			if(!reg.test(phone)){
        that.setData({
          msgText:"请输入正确的11位手机号!",
          msgHidden:false
        })
				return false;
			}else{
				wx.request({
					url:app.globalData.path+"/wish/yanzhengPhone",
					type:"GET",
					async:false,
					data:{yPhone : phone},
					datatype: "json",
					success: function (res){
            console.info(res)
						if(res.data == 1){
              that.setData({
              msgText:"该手机号已被注册，请重新输入!",
              msgHidden:false
            })
							bol = false;
						}else{
							that.setData({
              msgHidden:false
            })
							bol = true;
						}
					}
				});				
			}
		}
  var  time=function(that) {
			if (wait == 0) {
				that.setData({
          verificationCodeText:"获取验证码"
        })
				wait = 60;
				flag = 0;
			} else {
        that.setData({
          verificationCodeText:""+wait + "s后重新发送"
        })			
				wait--;
				flag = 1;
				setTimeout(function() {
						time(that);
					},
					1000);
			}
		}

Page({
  data: {
    userInfo: {},
    wishUser:{},
    activeCodeHidden:true,
    bangdingHidden:true,
    msgHidden:true,
    msgText:"",
    verificationCodeText:"获取验证码",
    code:"",
    codeType:"",
    errorText:"",
    errorHidden:true,
  },
 
  onLoad: function (options) {
    currentLocation = options.currentLocation;
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
      console.log(userInfo)
      getWishUser(that);
    })
  },
  //开通会员框
  payWay:function(){
    var that = this
    // wx.showActionSheet({
    // itemList: ['立即购买/立即续费', '激活码购买'],
    // success: function(res) {
    //   if (!res.cancel) {
      
    //     if(res.tapIndex === 0){
    //       wx.navigateTo({
    //         url: '../payOrder/payOrder?cardType=3'
    //       })
    //     }else{
    //       //激活码购买
    //       that.setData({
    //         activeCodeHidden: false
    //       });
    //     }
    //   }
    // }
    // })
    wx.navigateTo({
      url: '../applyCard/applyCard',
    })
  },
  cancel: function(){
    this.setData({
       couponHidden: true,
       activeCodeHidden:true
    });
  },
  //判断是优惠码或激活码
  checkType:function(e){
    console.log(e)
    var code = e.detail.value;
    var firstNum = code.substring(0,1);
    var strExp = /^[A-Za-z]+$/;
    if(code){
    if(strExp.test(firstNum)){
      console.log("是优惠码");
      this.setData({
        codeType:"1",
        code:code
      })
    }else{
      console.log("是激活码");
      this.setData({
        codeType:"2",
        code:code
      })
    }
   }
  },
  //激活
  actviePay:function(e){
    var that=this
    if(that.data.code){
    if(that.data.codeType ==="1"){
      //优惠码
      wx.request({
        url: app.globalData.path+'/weixin/coffee/xcx/user/voucherInfo',
        data: {
          voucherCode : that.data.code,
          userId:that.data.userInfo.userId
        },
        method: 'GET', 
        success: function(res){
          console.info(res)
            if(res.data.expired==false){
               wx.navigateTo({
                  url: '../payOrder/payOrder?cardType=3'
                })
					  }else{
							  console.info("优惠券无效");
                that.setData({
                activeCodeHidden:true,
                errorText:"优惠券无效",
                errorHidden:false
              })
					  }
        }
      })
    }else if(that.data.codeType ==="2"){
      //激活码
      console.info("激活码激活")
      wx.request({
        url: app.globalData.path+'/wish/applyCard',
        data: {
           userId:that.data.userId,
			     applyWay:1,
			     activateCode:that.data.code
        },
        method: 'GET',
        success: function(res){
          console.info(res);
	        if(res.code==0){
			        var msg="恭喜您！\r\n已开通WISH VIP身份";
			        console.info(msg);
			     }else{
			        that.setData({
                activeCodeHidden:true,
                errorText:res.data.msg,
                errorHidden:false
              })
			     }
        }
        
      })
    }
   } 
  },
  //绑定手机start
  closeDialog:function(){
  	this.setData({
  		bangdingHidden:true
  	})
  },
  getPhone:function(e){
    phone = e.detail.value; 
  },
  getCode:function(e){
    verificationCode = e.detail.value;
  },
  getVerificationCode:function() {
    console.info("----------------")
    var that = this
					if(flag == 0) {
           checkPhone(that);
					if(!bol){
						return;
					}
					time(that);
					wx.request({
							type : "GET",
							url : app.globalData.path+"/wish/getSms?type=0",
							data : {
								phone : phone
							},
							dataType : "json",
							success : function(res) {
                console.info(res)
								verificationCodeInSession=res.data.number;
							},
							error : function(data) {
							}
						})
					}
				},
    bindPhone:function(){
      var that=this
      if(phone ==""){
          this.setData({
            msgText:"请填写手机号!",
            msgHidden:false
          })
					}else if(verificationCode==""){
            this.setData({
            numText:"请填写验证码!",
            numHidden:false
          })
					}else if(phone =="" && verificationCode==""){
            this.setData({
            msgText:"请填写手机号!",
            msgHidden:false,
            numText:"请填写验证码!",
            numHidden:false
          })
					}else{
            wx.request({
              url: app.globalData.path+"/user/setPhone",
              data: {
                userId:this.data.userInfo.userId,
                phone:phone,
                verificationCode:verificationCode,
              verificationCodeInSession:verificationCodeInSession
              },
              method: 'GET', 
              success: function(res){
                that.setData({
                   bangdingHidden:true
                 })
                getWishUser(that);
              }
            })
          }
    },
 //绑定手机end 
 //联系客服
 callCustomservice:function(){
    wx.makePhoneCall({
      phoneNumber: "010-85373817"
    }) 
   },
    //绑定手机号
 bangding:function(){
   var that =this
   that.setData({
  		bangdingHidden:false
   })
   },
   closeErrorDialog:function(){
   this.setData({
     errorHidden:true
   })
 },
 //去已消费特权
 toGone:function(e){
   var count = e.currentTarget.dataset.count;
   if(count == 0){
      this.setData({
        errorText:"暂无已消费特权",
        errorHidden:false
      })
   }else{
     wx.navigateTo({
       url: '../gone/gone?currentLocation='+currentLocation
     })
   }
 }

})